#include <stdio.h>
#include <stdlib.h>
#include "Util/Util.h"
#include "Game/Game.h"
#include <GL/glut.h>


#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")


//Render callback
void AppRender()
{  
  //Render current state
  cGame::Get().Render();
}

//Iddle callback (update the state of the game)
void AppIdle()
{
  //If the game has finished
  if (cGame::Get().HasFinished())
  {
    //Deinit game and exit
    cGame::Get().Deinit();
    exit(0);
  }

  //Update game state
  static float lfLastTime = 0;
  static float lfActualTime;
  lfActualTime = GetMiliSeconds(); 
  float lfTimeStep = ((float)(lfActualTime - lfLastTime) / 1000.0f);
 
  lfLastTime = lfActualTime;

  

  cGame::Get().Update(lfTimeStep);
    //Render current state
  cGame::Get().Render();
}

//Process normal keys pressed
void AppKeyboard(unsigned char key, int x, int y)
{
  struct tInputState lState;
  lState.miKey = key;
  lState.miPosx = x;
  lState.miPosy = y;
  lState.mbPressed = true;
  lState.miButton = -1;
  lState.miState = -1;

  cInputManager::Get().SetState(lState);
}

//Process normal keys up
void AppKeyboardUp(unsigned char key, int x, int y)
{
  struct tInputState lState;
  lState.miKey = key;
  lState.miPosx = x;
  lState.miPosy = y;
  lState.mbPressed = false;
  lState.miButton = -1;
  lState.miState = -1;

  cInputManager::Get().SetState(lState);
}

//Process special keys pressed
void AppSpecialKeys(int key, int x, int y)
{
  struct tInputState lState;
  lState.miKey = key;
  lState.miPosx = x;
  lState.miPosy = y;
  lState.mbPressed = true;
  lState.miButton = -1;
  lState.miState = -1;

  cInputManager::Get().SetState(lState);
}

//Process special keys up
void AppSpecialKeysUp(int key, int x, int y)
{
	struct tInputState lState;
  lState.miKey = key;
  lState.miPosx = x;
  lState.miPosy = y;
  lState.mbPressed = false;
  lState.miButton = -1;
  lState.miState = -1;

  cInputManager::Get().SetState(lState);
}

int main(int argc, char* argv[])
{

  //Initializate glut
  glutInit(&argc, argv);

  //Init display
  glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA | GLUT_DOUBLE);

  cGraphicManager::Get().Init();

  int res_x = glutGet(GLUT_SCREEN_WIDTH);
	int res_y = glutGet(GLUT_SCREEN_HEIGHT);
  int pos_x = (res_x>>1)-(cGraphicManager::Get().GetGameWidth()>>1);
  int pos_y = (res_y>>1)-(cGraphicManager::Get().GetGameHeight()>>1);
	pos_x = 0;
  pos_y =600;
  //Create window
	glutInitWindowPosition(pos_x,pos_y);
  glutInitWindowSize(cGraphicManager::Get().GetGameWidth(), cGraphicManager::Get().GetGameHeight());
	glutCreateWindow("Game");

	//Register callback functions
	glutDisplayFunc(AppRender);			
	glutKeyboardFunc(AppKeyboard);		
	glutKeyboardUpFunc(AppKeyboardUp);	
	glutSpecialFunc(AppSpecialKeys);	
	glutSpecialUpFunc(AppSpecialKeysUp);
	glutIdleFunc(AppIdle);

	//Game initializations
	cGame::Get().Init();

	//Application loop
	glutMainLoop();	
}

